package com.example.springboot.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;


import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;

public class HttpUtil {


    private final HttpHeaders HEADERS = new HttpHeaders();
    private final SimpleClientHttpRequestFactory REQUEST_FACTORY = new SimpleClientHttpRequestFactory();
    static Logger log = LoggerFactory.getLogger(HttpUtil.class.getName());


    //获取所有的参赛用户信息
    public String GetAllMemberInfo(String Url) {
        REQUEST_FACTORY.setConnectTimeout(120000);
        REQUEST_FACTORY.setReadTimeout(120000);
//        REQUEST_FACTORY.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("172.67.75.139", 443)));
        RestTemplate restTemplate = new RestTemplate(REQUEST_FACTORY);
//        HEADERS.set("Authorization", "366B443B-E6DA-447C-AB44-2F4031D6DDDF");
        HEADERS.set("Cookie", "ASP.NET_SessionId=mhen3zlla3ytkjoueqeggkc");
        HEADERS.set("Accept", "*/*");
        HEADERS.set("Connection", "keep-alive");
        HEADERS.set("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
        HEADERS.set("Host", "www.worlddancesport.org");
        String body = restTemplate.exchange(Url, HttpMethod.GET, new HttpEntity<JSONObject>(HEADERS)
                , String.class).getBody();
        Document doc = Jsoup.parse(body);

        Element body1 = doc.body();
        Elements paragraphs = body1.getElementsByTag("table");
        List<Node> s = paragraphs.get(0).childNodes().get(1).childNodes().get(0).childNodes();
        if(s.size()>1){
            return "您的会员号已激活，可以报名";
        }else {
            return "您的会员号未激活，请等待审批";
        }
    }
    public static void main(String[] args) {
        String url =  "https://www.worlddancesport.org/Athlete/List?nameFilter=1000376&divisionFilter=General&divisionFilter" +
                "=Professional&divisionFilter=HipHop&divisionFilter=Breaking&divisionFilter=Disco&divisionFilter=" +
                "Stage&divisionFilter=Caribbean&divisionFilter=Smooth&divisionFilter=SoloSyncroChoreo&genderFilter=" +
                "Male&genderFilter=Female&statusFilter=Active&memberCountryFilter=-1&ageGroupFilter=221&ageGroupFilter=" +
                "180&ageGroupFilter=179&ageGroupFilter=177&ageGroupFilter=178&ageGroupFilter=220&ageGroupFilter=197" +
                "&ageGroupFilter=198&ageGroupFilter=199&ageGroupFilter=200&ageGroupFilter=201&ageGroupFilter=202&" +
                "ageGroupFilter=203&ageGroupFilter=204&ageGroupFilter=213&ageGroupFilter=175&ageGroupFilter=176&" +
                "profileFilter=false&formAction=go&Column=Id&Direction=Ascending&page=1&pageSize=25";
        HttpUtil httpUtil= new HttpUtil();
        httpUtil.GetAllMemberInfo(url);
    }
}
